<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="zh-cn" style="height: 100%">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
    <title><?=VIEW_TITLE?></title>
    <link href="<?php echo base_url('/assets/bs/'); ?>css/bootstrap.min.css" rel="stylesheet">
    <link href="<?php echo base_url('/assets/css/'); ?>my_common.css" rel="stylesheet">
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <style>
        .del:hover {
            transform: scale(1.3, 1.3);
        }

        .del {
            transition: all 0.3s;
        }
    </style>
</head>
<body>
<?php
$this->load->view('admin_header.php');
?>
<div class="container">
    <div id="main_div" style="max-width: 720px;padding-bottom:100px;margin: auto;">
        <div id="scroll_div" style="overflow-x: hidden;">
            <div id="task_div">
            </div>
            <div>
                <nav style="text-align: center;">
                    <ul class="pagination" id="page_ul">
                    </ul>
                </nav>
            </div>
        </div>

    </div>
</div>

<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" id="myModal"
     aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-body">
                是否返回未领取状态?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger btn-sm" onclick="changeReceivedStatus()">确认</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"
     id="myModal2"
     aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-body">
                确认删除改项目?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger btn-sm" onclick="deleteTask()">确认</button>
            </div>
        </div>
    </div>
</div>

<div class="hidden-xs">
<?php
$this->load->view('footer.php');
?>
</div>
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="<?php echo base_url('/assets/bs/'); ?>js/bootstrap.min.js"></script>
<script>
    var w_width = document.documentElement.clientWidth;
    var w_height = document.documentElement.clientHeight;
    var scroll_div_height = w_height - 180;
//  $('#scroll_div').height(scroll_div_height + 'px');
    var this_page = 1;
    var this_rows = 7;
    var this_page_sum = 0;
    var this_del_id = 0;
    var this_manager_id = 0;
    var this_translation_article_id = 0;


    function setChangeReceivedStatusInformation(manager_id, translation_article_id) {
        this_manager_id = manager_id;
        this_translation_article_id = translation_article_id;
        $('#myModal').modal('show');
    }
    function changeReceivedStatus() {

        $.ajax({
            url: '<?php echo site_url('Administrator/changeReceivedStatus');?>',
            type: 'post',
            dataType: 'json',
            data: {
                manager_id: this_manager_id,
                translation_id: this_translation_article_id
            },
            success: function (data) {
                if (data == 'success') {
                    $('#myModal').modal('hide');
                    $('#btn_receive_' + this_manager_id).text('未领取');
                    $('#btn_receive_' + this_manager_id).attr('disabled', 'disabled');
                    $('#btn_receive_' + this_manager_id).attr('class', 'btn btn-default');
                    this_manager_id = 0;
                    this_translation_article_id = 0;
                }
//                console.log(data);
            }
        });
    }

    function getTaskJson() {
        $.ajax({
            url: '<?php echo site_url('Administrator/getTaskJson');?>',
            type: 'post',
            dataType: 'json',
            data: {
                page: this_page,
                rows: this_rows
            },
            success: function (data) {
//                console.log(data);
                buildTask(data['list']);
                buildPageUl(data['count_all']);
                //设置cookie
                if (typeof setCookie === 'function') {
                    setCookie('administrator_task_page', this_page , 'h24');
                } else {
                    console.log('setCookie is not exist!');
                }
            }
        });
    }
    function getPage(page) {
        this_page = page;
        getTaskJson();
    }

    function nextPage() {
        if (this_page < this_page_sum) {
            ++this_page;
        }
        getPage(this_page);
    }
    function lastPage() {
        if (this_page != 1) {
            --this_page;
        }
        getPage(this_page);
    }

    var pagination_show_num = 7;//每一页显示的页码数量
//    function pagination_col(page_num) {
//        var begin = 0;
//        var finish = 0;
//        if (page_num > pagination_show_num) {//需要显示更多了
//            console.log(this_page % pagination_show_num);
//            if (this_page != 0 && this_page % pagination_show_num == 0) {//要切换分页组了
//                begin = this_page;
//                finish = this_page + pagination_show_num;
//                for (var i = begin; i <= finish; i++) {
//                    $('#pagination_' + i).show();
//                }
//            }else{
//                finish = Math.ceil(this_page/pagination_show_num) * pagination_show_num;
//                begin = finish - 7;
//                for (var i = begin; i <= finish; i++) {
//                    $('#pagination_' + i).show();
//                }
//            }
//        } else {
//            for (var i = 1; i <= 7; i++) {
//                $('#pagination_' + i).show();
//            }
//        }
//    }
    function pagination_col(page_num) {
        var begin = 0;
        var finish = 0;
        if (page_num > pagination_show_num) {//需要显示更多了
//            console.log(this_page % pagination_show_num);
            if (this_page != 0 && this_page % pagination_show_num == 0) {//要切换分页组了
                begin = this_page;
                finish = (this_page * 1 + pagination_show_num) * 1;

            } else {
                finish = Math.ceil(this_page / pagination_show_num) * pagination_show_num;
                begin = finish - 7;
            }
            if(finish > page_num){
                finish = page_num;
            }
            //判断是否还有下一组
            if ((finish - begin) < 7){
                $("#shenglue_page").hide();
            }
            for (var i = begin; i <= finish; i++) {
                $('#pagination_' + i).show();
            }
        } else {
            for (var i = 1; i <= 7; i++) {
                $('#pagination_' + i).show();
            }
        }
    }
    function buildPageUl(count_all) {
        $('#page_ul').empty();
        var page_num = Math.ceil(count_all / this_rows);
        this_page_sum = page_num;
        var html = '';
        html += '<li><a href="javascript:getPage(1)">&laquo;</a></li><li class=""><a href="javascript:lastPage()">上一页</a></li>';
        for (var i = 0; i < page_num; i++) {
            if (this_page == (i + 1)) {
                html += '<li style="display:none" id="' + "pagination_" + (i + 1) + '" class="active"><a href="javascript:getPage(' + (i + 1) + ')">' + (i + 1) + '</a></li>';
            } else {
                html += '<li style="display:none" id="' + "pagination_" + (i + 1) + '" ><a href="javascript:getPage(' + (i + 1) + ')">' + (i + 1) + '</a></li>';
            }
        }
        if (pagination_show_num < page_num) {
            html += '<li class="" id="shenglue_page"><a href="javascript:nextPage()">...</a></li>';
        }
        html += '<li class=""><a href="javascript:nextPage()">下一页</a></li><li><a href="javascript:getPage(' + page_num + ')">&raquo;</a></li>';
        $('#page_ul').append(html);
        pagination_col(page_num);
    }

    function buildTask(task_list) {
        var html = '';
        var len = task_list.length;
        for (var i = 0; i < len; i++) {
            html += '<div style="position: relative;" class="panel panel-default" id="task_main_div_' + task_list[i]['id'] + '">';
            html += '<span class="badge" style="position: absolute;top:10px;left:10px;">' + task_list[i]['id'] + '</span>';
            html += '<div class="panel-body" style="position: relative;">';
            html += '<div style="text-align: center;clear:both;padding-top:15px;">' + task_list[i]["title"] + '</div>';
            if (task_list[i]['yunying_title'] && task_list[i]['yunying_title'] != null && task_list[i]['yunying_title'] != '') {
                html += '<div style="text-align:center;font-size: 10px;">运管题目:' + task_list[i]['yunying_title'] + '</div>'
            }
            html += '<div class="del" style="position: absolute;top: 10px;right: 10px;" onclick="confirmDel(' + task_list[i]["id"] + ')"><span class="glyphicon glyphicon-trash"></span></div>';
            if (task_list[i]['is_proofreaded'] != 2) {
                html += '<div style="float:left;text-align: center;margin-top: 10px;width: 33.3%;">';
                if (task_list[i]['is_received'] == 0) {
                    html += '<button type="button" class="btn btn-default" disabled="disabled" style="width: 82px;">未领取</button>';
                } else if (task_list[i]['is_received'] == 1) {
                    html += '<button id="btn_receive_' + task_list[i]["id"] + '" type="button" class="btn btn-default btn-danger" style="width: 82px;" data-target=".bs-example-modal-sm" onclick="setChangeReceivedStatusInformation(' + task_list[i]["id"] + ',' + task_list[i]["translation_article_id"] + ')">已领取</button>';
                } else if (task_list[i]['is_received'] == 2) {
                    html += '<button type="button" class="btn btn-default btn-default" disabled ="disabled" style ="width: 82px;">已领取</button>';
                }
                html += '</div>';
                html += '<div style = "float:left;text-align:center;margin-top:10px;width:33.3%;">';
                if (task_list[i]['is_translationed'] == 0) {
                    html += '<button type = "button" class="btn btn-default" disabled="disabled" style="width:82px;">未翻译</button>';
                } else if (task_list[i]['is_translationed'] == 1) {
                    html += '<button type="button" class= "btn btn-default btn-default" disabled="disabled" style="width:82px;">翻译中</button>';
                } else if (task_list[i]['is_translationed'] == 2) {
                    html += '<button type = "button" class= "btn btn-default btn-default" disabled="disabled" style="width: 82px;">已翻译</button>';
                }
                html += '</div>';
                html += '<div style="float:left;text-align:center;margin-top:10px;width:33.3%;">';
                if (task_list[i]['is_proofreaded'] == 0) {
                    html += '<button type="button" class="btn btn-default btn-default" disabled="disabled" style="width: 82px;">未校对</button>';
                } else if (task_list[i]['is_proofreaded'] == 1) {
                    html += '<button type="button" class="btn btn-default btn-default" disabled="disabled" style="width: 82px;">校对中</button>';
                } else if (task_list[i]['is_proofreaded'] == 2) {
                    html += '<button type="button" class="btn btn-default btn-default" disabled="disabled" style="width: 82px;">已校对</button>';
                }
                html += '</div>';
            } else {
                html += '<div class="row" style="line-height: 30px;text-align: center;">';
                html += '<span class="col-xs-4 col-sm-4 col-md-4" style="">分数:' + task_list[i]['score'] + '</span>';
                html += '<span class="col-xs-4 col-sm-4 col-md-4" style="">译员名:' + task_list[i]['ta_name'] + '</span>';
                html += '<span class="col-xs-4 col-sm-4 col-md-4" style="">任务专员名:' + task_list[i]['oa_name'] + '</span>';
                html += '<span class="col-xs-4 col-sm-4 col-md-4" style=""><a href="javascript:readfangyi(' + task_list[i]['id'] + ',' + task_list[i]['is_received'] + ',' + task_list[i]['is_translationed'] + ',' + task_list[i]['is_proofreaded'] + ');">查看翻译</a></span>';
                html += '<span class="col-xs-4 col-sm-4 col-md-4" style=""><a href="javascript:jiaodui(' + task_list[i]['id'] + ',' + task_list[i]['is_translationed'] + ',' + task_list[i]['is_proofreaded'] + ')">查看校对</a></span>';
                html += '</div>';
            }
            html += '</div>';
            if (task_list[i]['is_proofreaded'] != 2) {
                html += '<div class="panel-heading" style="text-align: right;background-color: white;font-size: 12px;">';
                html += '<span style="display: block;float: left;font-size: 12px;">' + '任务发布时间:' + task_list[i]['oa_creat_time'] + '</span>';
                html += '<span style="display:block;">by:' + task_list[i]['oa_name'] + '</span>';
                if (task_list[i]['translation_article_id'] != 0) {
                    html += '<span style="display:block;">to:' + task_list[i]['ta_name'] + '</span>';
                }
                html += '</div>';
            }
            html += '</div>';
        }
        $('#task_div').empty();
        $('#task_div').append(html);
    }

    function confirmDel(id) {
        this_del_id = id;
        $('#myModal2').modal('show');
    }
    function deleteTask() {
        $.ajax({
            url: '<?php echo site_url('Administrator/deleteTask');?>',
            type: 'post',
            dataType: 'json',
            data: {
                id: this_del_id
            },
            success: function (data) {
                $('#myModal2').modal('hide');
                console.log(data);
                if (data == 'success') {
                    $('#task_main_div_' + this_del_id).hide();
                }
                this_del_id = 0;
            }
        });
    }

    function readfangyi(manager_id, is_received, is_translationed, is_proofreaded) {
        window.location.href = "<?php echo site_url('administrator/translation')?>" + "?manager_id=" + manager_id;
    }

    function jiaodui(manager_id, is_translationed, is_proofreaded) {
        if (is_translationed != 2) {
            alert('该文章译员尚未翻译完毕,请等待译员提交!');
            return false;
        }
        window.location.href = "<?php echo site_url('administrator/proofread?manager_id=')?>" + manager_id;
    }

    if (typeof getCookie === 'function') {
        var cookie_page = getCookie('administrator_task_page');
        if (cookie_page != '' && !isNaN(cookie_page)) {
            this_page = cookie_page;
        }
    } else {
        console.log('getCookie is not exist!');
    }
    getPage(this_page);

</script>
</body>
</html>